//
//  BLSettingTableViewCell.m
//  新浪微博
//
//  Created by hbl on 16/3/9.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLSettingTableViewCell.h"
#import "BLItem.h"
#import "BLBadgeButton.h"
@interface BLSettingTableViewCell()
@property (nonatomic, weak)UIImageView *bgImageView;
@property (nonatomic, weak) UITableView *tableView;

@property (nonatomic, strong)UISwitch *swith;
@property (nonatomic, strong)BLBadgeButton *badgeButton;
@property (nonatomic, strong)UIImageView *arrowImage;
@end

@implementation BLSettingTableViewCell

- (UISwitch *)swith
{
    if (!_swith) {
        _swith = [[UISwitch alloc]init];
    }
    return _swith;
}

- (BLBadgeButton *)badgeButton
{
    if (!_badgeButton) {
        _badgeButton = [[BLBadgeButton alloc]init];
    }
    return _badgeButton;
}

- (UIImageView *)arrowImage
{
    if (!_arrowImage) {
        _arrowImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_icon_arrow"]];
        _arrowImage.contentMode = UIViewContentModeCenter;
    }
    return _arrowImage;
}



+(instancetype)cellWithTableView:(UITableView *)tableView
{
    NSString *ID = @"cell";
    BLSettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[BLSettingTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
        cell.tableView = tableView;
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIImageView *bgImageView = [[UIImageView alloc]init];
        self.backgroundView = bgImageView;
        self.bgImageView = bgImageView;
        self.backgroundColor = [UIColor clearColor];
        self.tableView.backgroundColor = [UIColor clearColor];
       
    }
    return self;
}

- (void)setFrame:(CGRect)frame
{
    frame.origin.x = 5;
    frame.size.width -= 10;
    [super setFrame:frame];
}

- (void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
    NSString *imgName = nil;
    NSInteger number = [self.tableView numberOfRowsInSection:indexPath.section];
    if (number == 1) {
        imgName = @"common_card_background_os7";
    }else if (indexPath.row == 0){
        imgName = @"common_card_top_background_os7";
    }else if (indexPath.row == number - 1){
        imgName = @"common_card_bottom_background_os7";
    }else {
        imgName = @"common_card_middle_background_os7";
    }
    UIImage *tempImg = [UIImage imageNamed:imgName];
   tempImg = [tempImg resizableImageWithCapInsets:UIEdgeInsetsMake(tempImg.size.height * 0.5, tempImg.size.width * 0.5, tempImg.size.height, tempImg.size.width * 0.5)];
    self.bgImageView.image = tempImg;
}

- (void)setItem:(BLItem *)item
{
    _item = item;
    self.textLabel.text = item.tite;
    self.detailTextLabel.text = item.subTitle;
    if (item.iconString) {
        self.imageView.image = [UIImage imageNamed:item.iconString];
    }
    
    [self setupRightView];
    
}

- (void)setupRightView
{
    if (self.item.type == BLItemTypeArrow) {
        self.accessoryView = self.arrowImage;
    }else if (self.item.type == BLItemTypeNumber){
        self.accessoryView = self.badgeButton;
        self.badgeButton.badgeValue = self.item.numberValue;
    }else if (self.item.type == BLItemTypeSwich){
        self.accessoryView = self.swith;
    }
        
}

@end
